package com.fulihui.wgj.admin.integration.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.builder.web.PageViewBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.enums.MaterialTypeEnum;
import com.fulihui.common.request.rpc.AppRequest;
import com.fulihui.common.request.rpc.PageAppRequest;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.result.web.Result;
import com.fulihui.common.util.DateUtils;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.admin.api.material.vo.MaterialNewsVO;
import com.fulihui.wgj.admin.api.subscribereply.request.SubscribeAdvertEditRequest;
import com.fulihui.wgj.admin.api.subscribereply.request.SubscribeAdvertPageRequest;
import com.fulihui.wgj.admin.api.subscribereply.vo.SubscribeReplyAdvertVO;
import com.fulihui.wgj.admin.integration.SubscribeAdverseServiceClient;
import com.fulihui.wgj.facade.dto.WxMaterialDTO;
import com.fulihui.wgj.facade.dto.WxSubscribeReplyAdvertDTO;
import com.fulihui.wgj.facade.request.PrimaryKeyRequest;
import com.fulihui.wgj.facade.request.material.WxMaterialPageQueryRequest;
import com.fulihui.wgj.facade.request.reply.WxSubscribeReplyAdvertSaveRequest;
import com.fulihui.wgj.facade.result.material.WxMaterialPageResult;
import com.fulihui.wgj.facade.result.reply.WxSubsribeAdvertPageResult;
import com.fulihui.wgj.facade.service.reply.WxSubscribeAdverseService;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author: Levon
 * @date: 2018-07-31 09:55
 */
@Service
public class SubscribeAdverseServiceClientImpl implements SubscribeAdverseServiceClient {

    @Reference(version = "1.0.0")
    private WxSubscribeAdverseService wxSubscribeAdverseService;

    @Override
    public List<SubscribeReplyAdvertVO> queryAdvertList(String appId) {
        AppRequest request = new AppRequest();
        request.setAppId(appId);
        SimpleResult<List<WxSubscribeReplyAdvertDTO>> simpleResult = wxSubscribeAdverseService.queryAdvert(request);
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            List<WxSubscribeReplyAdvertDTO> datas = simpleResult.getData();
            if (CollectionUtils.isEmpty(datas)) {
                return BeanConv.copy(datas, SubscribeReplyAdvertVO.class);
            }
        }
        return null;
    }

    @Override
    public Result<Boolean> del(Integer id) {
        PrimaryKeyRequest request = new PrimaryKeyRequest();
        request.setId(id);
        BaseResult baseResult = wxSubscribeAdverseService.deleteReplyAdvertById(request);
        if(RpcResultCheckUtil.isSuccess(baseResult)){
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(),baseResult.getErrmsg());
    }

    @Override
    public Result<Boolean> edit(SubscribeAdvertEditRequest request) {
        WxSubscribeReplyAdvertSaveRequest saveRequest = new WxSubscribeReplyAdvertSaveRequest();
        saveRequest.setAdvert(request.getAdvert());
        saveRequest.setAppId(request.getAppId());
        saveRequest.setId(request.getId());
        BaseResult baseResult = wxSubscribeAdverseService.saveReplyRule(saveRequest);
        if(RpcResultCheckUtil.isSuccess(baseResult)){
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(),baseResult.getErrmsg());
    }

    @Override
    public PageView<SubscribeReplyAdvertVO> queryPage(SubscribeAdvertPageRequest request) {
        PageAppRequest pageQueryRequest = new PageAppRequest();
        pageQueryRequest.setAppId(request.getAppId());
        pageQueryRequest.setPage(request.getPage());
        pageQueryRequest.setRows(request.getRows());

        List<SubscribeReplyAdvertVO> advertVOS = Lists.newArrayList();

        WxSubsribeAdvertPageResult pageResult = wxSubscribeAdverseService.pageQueryAdvert(pageQueryRequest);
        if (RpcResultCheckUtil.isSuccess(pageResult)) {
            List<WxSubscribeReplyAdvertDTO> values = pageResult.getValue();
            if (!CollectionUtils.isEmpty(values)) {
                advertVOS = BeanConv.copy(values, SubscribeReplyAdvertVO.class);
            }
        }
        return PageViewBuilder.build(advertVOS, pageResult.getPage(), pageResult.getRows(), pageResult.getTotalRows());
    }

    @Override
    public Result<SubscribeReplyAdvertVO> queryById(Integer id) {
        SubscribeReplyAdvertVO advertVO = null;
        PrimaryKeyRequest request = new PrimaryKeyRequest();
        request.setId(id);
        SimpleResult<WxSubscribeReplyAdvertDTO> simpleResult = wxSubscribeAdverseService.queryAdvert(request);
        if(RpcResultCheckUtil.isSuccess(simpleResult)){
            WxSubscribeReplyAdvertDTO data = simpleResult.getData();
            if(data != null){
                advertVO = BeanConv.copy(data, SubscribeReplyAdvertVO.class);
            }
        }
        return JsonResultBuilder.succ(advertVO);
    }

}
